package com.iceengine.input.keyboard;

/**
 * The keyboardevent listener is used to listen what happens at the keyboard.
 * @author penguin
 */
import org.lwjgl.input.Keyboard;
import java.util.ArrayList;

public class KeyboardListener implements Runnable {
    private ArrayList<IKeyboardEvent> Events;
    private boolean running;
    public void run()
    {
        running = true;
        Events = new ArrayList<IKeyboardEvent>();
        while(running)
        {
            int[] tempKeys;

            for(int i = 0; i < Events.size(); i++)
            {
                tempKeys = Events.get(i).getKeysToWaitFor();
                for(int ki = 0; ki < tempKeys.length; ki++)
                {
                    if(Keyboard.isKeyDown(tempKeys[ki]))
                        Events.get(i).KeyIsDown(tempKeys[ki]);
                }
            }

            try
            {
                Thread.currentThread().sleep(20);
            }
            catch(Exception e)
            {

            }
        }
        Thread.currentThread().suspend();
    }
    /**
     * Kills the thread. This stops listening!
     */
    public void killThread()
    {
        running = false;
    }
    /**
     * Add an event to the listener.
     * @param toAdd Event to add.
     */
    public void addEvent(IKeyboardEvent toAdd)
    {
        Events.add(toAdd);
    }
    /**
     * Remove an event from the listener.
     * @param toRemove Event to remove.
     */
    public void removeEvent(IKeyboardEvent toRemove)
    {
        Events.remove(toRemove);
    }
}
